//
//  RiskPol.js
//  SugLisSystem
//
//  Created by Mr.Right on 2013-04-02.
//  Copyright 2013 Mr.Right. All rights reserved.
//

var dB = require('untils/DB');
/**
 * 获取险种保单数据链
 * @param mainpolNo 不一定是主险保单，如果是附加险，返回含有附加险的附加险的保单数据链
 */
function getPolList(mainPolNo, insuredNo, serialNo, flag) {
	var list = [];
	var polList = [];
	var noList = [];

	var LSPolDB = new dB('LSPol');
	LSPolDB['polno'] = mainPolNo;
	if (LSPolDB.getInfo()) {
		polList.push(LSPolDB.getSchema());
		if (flag && LSPolDB['insuredno'] == insuredNo) {
			noList.push("F" + serialNo);
		} else if (!flag && LSPolDB['insuredno'] == insuredNo) {
			noList.push(serialNo);
		}

		var subLSPolSet = getSubPolInfo(mainPolNo);
		var subCount = 1;
		for (var i = 0; i < subLSPolSet.length; i++) {
			var tempLSPolSchema = subLSPolSet[i];
			polList.push(tempLSPolSchema);
			if (flag && LSPolDB['insuredno'] == insuredNo) {

				noList.push("F" + serialNo + '.' + subCounts);
			} else if (!flag && LSPolDB['insuredno'] == insuredNo) {
				noList.push(serialNo + '.' + subCounts);
			}
			// 查看是否存在附加险的附加险
			var subsubLSPolSet = getSubPolInfo(tempLSPolSchema['polno']);
			var subSCounts = 1;
			for (var m = 0; m < subsubLSPolSet.length; m++) {
				var tempsubLSPolSchema = subsubLSPolSet[m];
				polList.push(tempsubLSPolSchema);
				if (flag && LSPolDB['insuredno'] == insuredNo) {
					noList.push("F" + subCounts + '.' + subSCounts);
				} else if (!flag && LSPolDB['insuredno'] == insuredNo) {
					noList.push(subCounts + '.' + subSCounts);
				}
				subSCounts++;
			}
			subCount++
		}
		list.push(noList);
		list.push(polList);
		return list;
	}

}

exports.getPolList = getPolList;

function getSubPolInfo(mainPolNo) {
	var sql = "select * from lspol where mainpolno<>polno and mainpolno='" + mainPolNo + "'";
	var LSPolDB = new dB('LSPol');
	return LSPolDB.executeQuery(sql);
}
